<?php

/**
 * 数据调用模块
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class IndexAction extends BaseAction {

    public function index() {
        $id = (int) $this->_get("id");
        $page = (int) $this->_get(C("VAR_PAGE"));
        $this->assign(C("VAR_PAGE"), $page);
        $data = S("Datacall_{$id}_{$page}");
        if ($data) {
            $this->sheader($data);
        } else {
            $db = M("Datacall");
            $r = $db->where(array("id" => $id, "status" => 1))->find();
            if ($r) {
                $r['setting'] = unserialize($r['setting']);
                $r['setting']['page'] = $page?$page:1;
                //取得数据
                $rs = array(
                    "id" => $r['id'],
                    "data" => $this->datatype($r['setting']),
                    "type" => $r['type'],
                    'cachetime' => $r['cachetime'],
                    'temptext' => $r['temptext'],
                    'page' => $page
                );
                
                if((int)$r['cachetime']){
                    S("Datacall_{$id}_{$page}", $rs, $r['cachetime']<60?60:$r['cachetime']);
                }
                
                $this->sheader($rs);
            }
        }
    }

    /**
     * 数据输出类型
     */
    protected function sheader($rs) {
        $type = $rs['type'];
        switch ($type) {
            case 1://javascript
                $cachepath = CACHE_PATH . GROUP_NAME . "/" . md5("Datacall_javascript" . $rs['id']) . ".html";
                if (!file_exists($cachepath)) {
                    file_put_contents($cachepath, strip_whitespace($rs['temptext']));
                }
                $this->assign("data", $rs['data']['data']);
                $this->display($cachepath);
                exit;
                break;
            case 2://json
                header("Content-Type: application/json; charset=" . C("DEFAULT_CHARSET"));
                $return = array(
                    "info" => $rs['data']['setting'],
                    "status" => true,
                    "data" => $rs['data']['data']
                );
                
                echo json_encode($return);
                exit;
                break;
            case 3://jsonp
                $callback = $this->_get("callback");
                $return = array(
                    "info" => $rs['data']['setting'],
                    "status" => true,
                    "data" => $rs['data']['data']
                );
                echo $callback . "(" . json_encode($return) . ");";
                exit;
                break;
        }
    }

    /**
     * 数据调用类型 
     */
    protected function datatype($setting) {

        //调用类型
        $type = $setting['type'];
        //信息数
        $limit = $setting['limit'];
        //排序
        $order = $setting['order'];
        //开启分页
        $setpage = $setting['page'];
        switch ($type) {
            case "category":
                $category = $setting['category'];
                $Cat = F("Category");
                $Model = F("Model");
                if (is_array($category)) {
                    //获取表名
                    $tablename = ucwords($Model[$Cat[$category[0]]['modelid']]['tablename']);
                    $db = M($tablename);
                    $where = array();
                    if (count($category) > 1) {
                        $where['catid'] = array("IN", implode(",", $category));
                    } else {
                        $where['catid'] = array("EQ", $category[0]);
                    }
                    $where['status'] = array("EQ", 99);
                    $count = $db->where($where)->count();
                    if ($setpage) {
                        $page = $this->page($count, $limit);
                        if ($setting['field']) {
                            $data = $db->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->getField($setting['field']);
                        } else {
                            $data = $db->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
                        }
                    } else {
                         if ($setting['field']) {
                            $data = $db->where($where)->order($order)->limit($limit)->getField($setting['field']);
                        } else {
                            $data = $db->where($where)->order($order)->limit($limit)->select();
                        }
                    }
                    $setting['count'] = $count;
                }
                //取得点击数和评论总数
                foreach($data as $k=>$v){
                    $data[$k]['hits'] = hits("c-".$v['catid']."-".$v['id']);
                    $data[$k]['commcount'] = commcount($v['catid'], $v['id']);
                }
                return array(
                    "data" => $data,
                    "setting" => $setting
                );
                break;
            case "model":
                $Model = F("Model");
                $modeid = $setting['modeid'];
                if ($Model[$modeid]) {
                    $db = M(ucwords($Model[$modeid]['tablename']));
                    $where = array();
                    $where['status'] = array("EQ", 99);
                    $count = $db->where($where)->count();
                    if ($setpage) {
                        $page = $this->page($count, $limit);
                        if ($setting['field']) {
                            $data = $db->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->getField($setting['field']);
                        }else{
                            $data = $db->where($where)->order($order)->limit($page->firstRow . ',' . $page->listRows)->select();
                        }
                    } else {
                         if ($setting['field']) {
                            $data = $db->where($where)->order($order)->limit($limit)->getField($setting['field']);
                        }else{
                            $data = $db->where($where)->order($order)->limit($limit)->select();
                        }
                    }
                }
                $setting['count'] = $count;
                return array(
                    "data" => $data,
                    "setting" => $setting
                );
                break;
            case "sql":
                $db = M('');
                if ($sql = preg_replace('/select([^from].*)from/i', "SELECT COUNT(*) as count FROM ", $setting['sql'])) {
                    $count = $db->query($sql);
                    $count = $count[0]['count'];
                    $sq = "";
                    if ($order) {
                        $sq .= "ORDER BY {$order} ";
                    }
                    if ($setpage) {
                        $page = $this->page($count, $limit);
                        $sq .= " LIMIT " . $page->firstRow . ',' . $page->listRows . " ";
                    }
                    $data = $db->query($setting['sql'] . $sq);
                }
                $setting['count'] = $count;
                unset($setting['sql']);
                return array(
                    "data" => $data,
                    "setting" => $setting
                );
                break;
        }
    }

}

?>
